<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 用户权限管理
 */
class Power extends Auth_Controller{

    function __construct()
    {
        parent::__construct();
        $this->load->model('power_mdl');
    }

    /**
     * 提交验证
     */
    private function validation($data){
        if(isset($data['pid'])){
            if(!is_numeric($data['pid'])) die("服务器内部错误");
        }
        $data['show'] = 0;
        if(isset($data['basis'])){
            if(!preg_match("/^[1|2|3|0]$/",$data['basis'])) die('服务器内部错误');
            if($data['basis'] == 1 || $data['basis'] == 0){
                if($data['pid'] == 0) $data['show'] = 1;
            }
        }

        if(isset($data['name'])){
            if(empty($data['name'])) die('权限名称不能为空');
            $data['menu'] = $data['name'];
        }
        return $data;
    }

    /**
     * 权限管理首页
     * 查询所有权限
     */
    public function index(){
        //查询所有权限(去除基本权限)
        $pid = $this->input->get('pid',TRUE);
        if(empty($pid)){
            $pid = 0;
        }
        $map = array(
            'pid' => $pid,
            'basis !=' => '2'
        );
        $list = $this->power_mdl->power_all($map);
        $data['list'] = $list;
        $this->_view('admin/power_index',$data);
    }

    //新增权限
    public function create(){
        $post = $this->input->post(NULL,TRUE);

        if(!empty($post) && $this->input->is_ajax_request()){
            $data = $this->validation($post);
            $v = $this->power_mdl->create($data);
			$this->logs->add($v,'新增权限"'.$data['name'].'"');
            echo $v === false ? '服务器错误，添加失败' : 1 ;
        }else{
            $map = array(
                'pid' => 0,
                'basis !=' => '2'
            );
            $list = $this->power_mdl->power_all($map);
            $data['list'] = $list;
            $this->_view('admin/power_create',$data);
        }

    }

    //权限修改
    public function update(){
        $post = $this->input->post(NULL,TRUE);

        if(!empty($post) && $this->input->is_ajax_request()){
            $data = $this->validation($post);
			if(empty($data['id'])){
				show_404();
				exit;
			}
			
            $v = $this->power_mdl->update($data,array('id'=>$data['id']));
			$this->logs->add($v,'修改权限"'.$data['name'].'"');
            echo $v === false ? '服务器错误，修改失败' : 1 ;
			
        }else{
        	
            $pid = $this->input->get('pid',TRUE);
            if(empty($pid) && is_numeric($pid)){
                show_404();
                exit;
            }
            $map = array(
                'basis !=' => '2'
            );
            $list = $this->power_mdl->power_all($map);
            //获取所有的上级权限
            $this->load->model("sort_mdl");
            $flist = $this->sort_mdl->get_fsort($pid,$list);
			
			//当前权限
			$data['res'] = $flist[0];
            unset($flist[0]);
			if(empty($flist)){
				$flist[0] = array(
					'id' => 0,
					'content' => $data['res']['content']
				);
			}
			//print_r($flist);exit;
            $data['list'] = $flist;
            
            $this->_view('admin/power_update',$data);
        }

    }
	
	/**
	 * 权限删除
	 */
	public function delete(){
		if(!$this->input->is_ajax_request()){
			show_404();
			exit();
		}
		
		$pid = $this->input->post('pid',TRUE);
		
		if(empty($pid)){
			die("内部错误！");
		}
		
		$this->db->trans_start();
		$this->power_mdl->delete(['id'=>$pid]);
		$v = $this->power_mdl->delete(['pid'=>$pid]);
		$this->db->trans_complete();
		
		$row = $this->power_mdl->get_one($pid);
		$this->logs->add($v,'删除权限"'.$row['name'].'"');
		
		echo $v === false ? '删除失败！' : 1 ;
	}
	
	

}